layui.use(['layer', 'form', 'table'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var table = layui.table;

    /**
     * 集团信息--集团智能网语音信息管理
     */
    var EcGroup = {
        tableId: "ecGroupTable",    //表格id
        condition: {
            accountId: ""
        }
    };

    /**
     * 初始化表格的列
     */
    EcGroup.initColumn = function () {
        return [
            // 一级表头
            [
            {field: 'groupId', align: "center", width:165, title: '群组ID', rowspan: 2},
            {field: 'groupName', align: "center", title: '群组名称', rowspan: 2},
            {field: 'offeringId', align: "center", width:120, title: '群组主资费ID', rowspan: 2},
            {field: 'offeringName', align: "center", title: '群组主资费名称', rowspan: 2},
            {align: "center", title: '本月套餐内流量共享/流量池使用情况（KB）',colspan: 3},
            {field: 'useAmount', align: "center", toolbar: '#gprsUseAmountBar', title: '本月累计GPRS使用量（KB）', rowspan: 2},
            {align: "center", toolbar: '#memberMgrBar',width:90, title: '成员管理', rowspan: 2},
            {align: 'center', toolbar: '#orderedOfferingsBar', width:100, title: '已订购资费', rowspan: 2},
            {align: 'center', toolbar: '#groupVoiceWhiteListBar', width:130, title: '群组语音白名单', rowspan: 2}
            ],
            // 二级表头
            [
                {field: 'totalAmount1', align: "center", width:110, title: '总量'},
                {field: 'useAmount1', align: "center", width:110, toolbar: '#shareUseAmountBar', title: '使用量'},
                {field: 'remainAmount1', align: "center", width:110, title: '剩余量'}
            ]
        ];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + EcGroup.tableId,
        defaultToolbar: [],
        url: Feng.ctxPath + '/ecGroup/list',
        page: true,
        limits: [10,20,30,40,50],
        height: "full-98",
        cellMinWidth: 100,
        cols: EcGroup.initColumn(),
        done:function(res, curr, count){
            // 鼠标显示过长的表头
            tdTitle();
        }
    });

    /**
     * 点击查询按钮
     */
    $("#btnSearch").click(function () {
        EcGroup.search();
    });
    EcGroup.search = function () {
        var queryData = {};
        var accountId = $("#accountName").val();
        queryData['accountId'] = accountId;
        table.reload(EcGroup.tableId, {
            where: queryData, page: {curr: 1}
        });
        initAccountSelect(accountId);
    };

    // 工具条点击事件
    table.on('tool(' + EcGroup.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'gprsUseAmount') {
            EcGroup.gprsUseAmountBar(obj);
        } else if (layEvent === 'memberMgr') {
            EcGroup.memberMgrBar(data.groupId);
        } else if (layEvent === 'orderedOfferings') {
            EcGroup.orderedOfferingsBar(data.groupId);
        } else if (layEvent === 'groupVoiceWhiteList') {
            EcGroup.groupVoiceWhiteListBar(data.groupId);
        } else if (layEvent === 'shareUseAmount') {
            EcGroup.shareUseAmountBar(obj);
        }
    });

    /**
     * 查看 本月累计GPRS使用量（KB）
     *
     * @param data 点击按钮时候的行数据
     */
    EcGroup.gprsUseAmountBar = function (obj) {
        var groupId = obj.data.groupId
        $.ajax({
            type: 'get',
            url: Feng.ctxPath + '/ecGroup/gprsUseAmount?groupId=' + groupId + '&accountId=' + $("#accountName").val(),
            async: false,
            success: function (data) {
                if (data.code != 0) {
                    layer.msg(data.msg);
                } else {
                    var useAmount = 0;
                    for (var i = 0; i < data.data.length; i++) {
                        useAmount += parseFloat(data.data[i].useAmount);
                    }
                    obj.update({
                        useAmount: useAmount
                    });
                }
            }
        });
    };

    /**
     * 群组成员管理
     *
     * @param data 点击按钮时候的行数据
     */
    EcGroup.memberMgrBar = function (groupId) {
        layer.open({
            type: 2,
            title: '群组成员管理',
            area: ['850px', '600px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/ecGroup/group_member/' + groupId + '/' + $("#accountName").val(),
            btn: ['关闭']
        });
    };

    /**
     * 查看 已订购资费
     *
     * @param data 点击按钮时候的行数据
     */
    EcGroup.orderedOfferingsBar = function (groupId) {
        layer.open({
            type: 2,
            title: '群组已订购资费',
            area: ['950px', '650px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/ecGroup/group_order/' + groupId + '/' + $("#accountName").val(),
            btn: ['关闭']
        });
    };

    /**
     * 查看 群组语音白名单
     *
     * @param data 点击按钮时候的行数据
     */
    EcGroup.groupVoiceWhiteListBar = function (groupId) {
        layer.open({
            type: 2,
            title: '群组语音白名单',
            area: ['1000px', '650px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/ecGroup/toGroupVoiceWhite/' + groupId + '/' + $("#accountName").val(),
            btn: ['关闭']
        });
    };

    /**
     * 查看 本月套餐内流量共享/流量池使用情况（KB）
     *
     * @param data 点击按钮时候的行数据
     */
    EcGroup.shareUseAmountBar = function (obj) {
        var groupId = obj.data.groupId
        $.ajax({
            type: 'get',
            url: Feng.ctxPath + '/ecGroup/shareUseAmount?groupId=' + groupId + '&accountId=' + $("#accountName").val(),
            async: false,
            success: function (data) {
                if (data.code != 0) {
                    layer.msg(data.msg);
                } else {
                    var useAmount = 0, remainAmount = 0, totalAmount = 0;
                    for (var i = 0; i < data.data.length; i++) {
                        useAmount += parseFloat(data.data[i].useAmount1);
                        remainAmount += parseFloat(data.data[i].remainAmount1);
                        totalAmount += parseFloat(data.data[i].totalAmount1);
                    }
                    obj.update({
                        useAmount1: useAmount,
                        remainAmount1: remainAmount,
                        totalAmount1: totalAmount
                    });
                }
            }
        });
    };

    initAccountSelect();

    /**
     * 初始化集团下拉列表框
     * @type {layui.ax}
     */
    function initAccountSelect(accountId) {
        $('#accountName').html('');
        $("#accountName").append('<option value="" >请选择</option>');
        $.ajax({
            dataType: 'json',
            type: 'post',
            url: Feng.ctxPath + "/account/allSelectAccounts",
            success: function (data) {
                for (var i = 0; i < data.data.length; i++) {
                    var name = data.data[i].accountName;
                    var code = data.data[i].accountId;
                    if (accountId == code) {
                        $("#accountName").append('<option value="' + code + '" selected>' + name + '</option>');
                    } else {
                        $("#accountName").append('<option value="' + code + '">' + name + '</option>');
                    }
                }
                form.render('select');
            }
        });
    }

    function tdTitle(){
        $('th').each(function(index,element){
            $(element).attr('title',$(element).text());
        });
        $('td').each(function(index,element){
            $(element).attr('title',$(element).text());
        });
    }
});